package com.cf.invoker.http.server;

import cn.hutool.core.collection.ConcurrentHashSet;
import com.cf.invoker.RemoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.util.Set;

/****
 *
 * @Author zhu-mingming
 * @Description
 * @Date 2018/10/23
 *
 **/

@Slf4j
public class RemoteServiceScanner implements BeanPostProcessor {

    private ServiceRegisterStore serviceRegisterStore;

    private Set<Object> registered = new ConcurrentHashSet<Object>();

    public RemoteServiceScanner(ServiceRegisterStore serviceRegisterStore) {
        this.serviceRegisterStore = serviceRegisterStore;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        RemoteService remoteService = bean.getClass().getAnnotation(RemoteService.class);
        if (remoteService != null && !registered.contains(bean)) {
            registered.add(beanName);
            serviceRegisterStore.registerServiceMetas(bean);
        }
        return bean;
    }
}
